package com.chenyuxin.ilp.controller.file;

import com.chenyuxin.ilp.model.pojo.Result;
import com.chenyuxin.ilp.service.file.FileService;
import com.chenyuxin.ilp.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/file/meta")
public class FileController {
    @Autowired
    private FileService fileService;
    // 添加文件
    @PostMapping("/add")
    public Result createFile(@RequestParam("parentid") Long parentId, @RequestPart MultipartFile file){
        String userId = SecurityUtil.getUserId();
        return fileService.addFile(userId,parentId,file);
    }
    // 查询所有学习资源的树型结构
    @GetMapping("/selectall")
    public Result selectFileTree(){
        String userId = SecurityUtil.getUserId();
        return fileService.selectFileTree(userId);
    }
    // 根据父节点id查询子节点
    @GetMapping("/select/{folderid}")
    public Result selectFileByParentId(@PathVariable("folderid")Long folderId){
        String userId = SecurityUtil.getUserId();
        return fileService.selectFileByParentId(userId,folderId);
    }
    // 删除文件
    @DeleteMapping("/delete/{fileid}")
    public Result deleteFile(@PathVariable("fileid") Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileService.deleteFile(userId,fileId);
    }
    // 批量删除文件
    @DeleteMapping("/deletebatch")
    public Result deleteFileBatch(@RequestParam List<Long> fileIds){
        String userId = SecurityUtil.getUserId();
        return fileService.deleteFileBatch(userId,fileIds);
    }
    // 文件预览
    @GetMapping("/preview/{fileid}")
    public Result previewFile(@PathVariable("fileid") Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileService.previewFile(fileId,userId);
    }
    // 文件下载
    @GetMapping("/download/{fileid}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable("fileid") Long fileId){
        String userId = SecurityUtil.getUserId();
        return fileService.downloadFile(userId,fileId);
    }
    // 修改文件权限
    @PostMapping("/permission")
    public Result updateFileOpen(@RequestParam("fileid") Long fileId, @RequestParam Integer permission){
        String userId = SecurityUtil.getUserId();
        return fileService.updateFileOpen(fileId,permission,userId);
    }
}
